﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SmartRFIDSystem19.dao;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace SmartRFIDSystem19.dao
{
    class AccountDao
    {
        //通过卡号在数据库的account表中查询姓名
        public static string getNameByCardId(string mfid)
        {
            string name = "";
            try
            {
                Connection.Connect();
                //MessageBox.Show("333");
               // MessageBox.Show(mfid+"mro");
                string sqlStr = "select name from account where mfid = '" + mfid + "'";
                Connection.comm = new MySqlCommand(sqlStr, Connection.conn);
                int r = Connection.comm.ExecuteNonQuery();
                //MessageBox.Show("r="+r);
                //name = Connection.comm.ExecuteScalar().ToString();
                object o = Connection.comm.ExecuteScalar();
                //MessageBox.Show(o.ToString());
                if (o != null)
                {
                    name = o.ToString();
                }
                if (r == -1)
                {
                    
                }
                else
                {
                    //name="新用户";
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine("发生了异常: " + ex.Message);
            }

            return name;
        }
        //通过卡号在数据库的account表中查询余额
        public static float getBalanceByCardId(string mfid)
        {
            float balance = 0;
            try
            {
                Connection.Connect();
                string sqlStr = "select balance from account where mfid = '" + mfid + "'";
                Connection.comm = new MySqlCommand(sqlStr, Connection.conn);
                Connection.comm.ExecuteNonQuery();
               // balance = float.Parse(Connection.comm.ExecuteScalar().ToString());
                object o = Connection.comm.ExecuteScalar();
                if (o != null)
                {
                    balance = float.Parse(o.ToString());
                }
            }
            catch
            {

            }
            return balance;
        }
        //修改数据中account表中的余额
        public static void updateBalanceByCardId(string mfid, float balance)
        {
            try
            {
                Connection.Connect();
                string sqlStr = "update account set balance = '" + balance + "' where mfid = '" + mfid + "'";
                Connection.comm = new MySqlCommand(sqlStr, Connection.conn);
                int r = Connection.comm.ExecuteNonQuery();
                if (r == 1)
                {
                    //MessageBox.Show("数据库账户余额改写成功");
                    return;
                }
                else
                {
                    MessageBox.Show("数据库账户余额改写失败");
                    return;
                }
            }
            catch
            {

            }
        }
    }
}
